import { show_mt, show_st, obj_int, obj_str, loc_dt, uDb, cline, ck_sn, add_ck_col, reload_col, loc } from 'sui';
import { User } from 'cache';
import { show_val } from 'buy/task/cmd';

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_488"), tp: "string"},
    ];
};

const load_col = (mod, setCol) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), mod, fix);
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => ck_sn(v, cur, main),
    };

    add_ck_col(ret, col);

    return ret;
};

const load_supply = (mod, setMain, mapSupply, tp="supply", gtp="group") => {
    uDb(null, `file/list/${tp}`, { line: cline() },
        supply => {
            if (mapSupply) {
                const map = {};
                supply.forEach(d => map[d.sn] = d.name);

                mapSupply(map);
            }

            if (setMain) {
                uDb(null, `file/get/${gtp}`, { line: cline(), sn: mod },
                    group => {
                        const set = new Set();
                        const ret = [];

                        if (group.user) {
                            group.user.forEach(d => set.add(d));
                        }

                        supply.forEach(d => {
                            if (set.has(d.sn)) {
                                ret.push(d);
                            }
                        });

                        setMain(ret);
                    },
                    e => setMain(null));
            }
        },
        e => {
            mapSupply({});

            if (setMain) {
                setMain(null);
            }
        });
};

const get_tp = (mod) => {
    if (mod === "cera") {
        return "T";
    } else if (mod === "stell") {
        return "G";
    } else if (mod === "cage") {
        return "C";
    } else if (mod === "seal") {
        return "S";
    } else {
        return mod;
    }
};

const add_pick = (mod, val, cur, umd) => {
    umd.startWait();

    const num = obj_int(val, "num");
    const dnum = obj_int(cur, "dnum");

    if (num > dnum) {
        umd.setErr(loc("l_753"));
        return;
    }

    uDb(null, "code/gen",
        {
            line: cline(),
            rule: "pick",
        },
        code => {
            const cmd = {
                "line": cline(),
                "v": {
                    "sn": obj_str(code, "psn"),
                    "usn": User.userName(),
                    "uname": User.uName(),
                    "bsn": obj_str(cur, "sn"),
                    "face": obj_str(cur, "face"),
                    "tech": obj_str(val, "tech"),
                    "state": "open",
                    "num": num,
                    "pstart": loc_dt(new Date()),
                    "mt": get_tp(mod),
                    "side": obj_str(val, "side"),
                }
            };

            uDb(null, "pick/add", cmd,
                r => {
                    umd.endWait();
                    alert(loc("l_754"));
                }, e => umd.setErr());
        }, e => umd.setErr(loc("l_755")));
};

const st_list = () => {
    return [
        {sn: "ok", name: show_st("ok")},
        {sn: "block", name: show_st("block")},
    ];
};

const get_col = (col, tmp, map_supply) => {
    col.forEach(d => {
        if (d.sn === "supply") {
            tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => show_val(props, "supply", map_supply)});
        } else {
            show_mt(tmp, d, 150);
        }
    });
};

export {get_col, st_list, get_tp, add_pick, load_supply, get_ck, load_col, fix_col };
